#include "querys.h"
#include <string.h>


int serialize_aeroplane(char** text, avion_t aeroplane){

	int size = sizeof(int)*3;

	int i;
	for(i=0 ; i<aeroplane->drogasCant ; i++){
		size += strlen(aeroplane->drogas[i]->nombre) + 1 + sizeof(int);
	}

	char* to_send= malloc( size );
	char* to_sendp=to_send;

	*(int*)to_sendp=aeroplane->ciudadDestinoId;
	to_sendp += sizeof(int);

	*((int*)to_sendp)=aeroplane->distancia;
	to_sendp += sizeof(int);

	*((int*)to_sendp)=aeroplane->drogasCant;
	to_sendp += sizeof(int);
	
	for(i=0 ; i<aeroplane->drogasCant ; i++){
		strcpy(to_sendp , aeroplane->drogas[i]->nombre);
		to_sendp += strlen(aeroplane->drogas[i]->nombre) + 1;

		*((int*)to_sendp)=aeroplane->drogas[i]->cant;
		to_sendp += sizeof(int);
	}
	*text=to_send;
	return size;

}

void send_min(int min){
	void* ans=NULL;
	make_query(SEND_MIN, &min, sizeof(int), &ans);
	if(ans)
		free(ans);
}

int get_min(){
	void* ans=NULL;
	make_query(GET_MIN, NULL, 0, &ans);
	int min;
	if(ans){
		min=*((int*)ans);
		free(ans);
	}
	return min;
}

void add_aeroplane_to_print(avion_t aeroplane){
	char* to_send;
	int size = serialize_aeroplane(&to_send, aeroplane);
	void* ans=NULL;
	make_query(ADD_AEROPLANE_TO_PRINT, to_send, size, &ans);
	if(ans)
		free(ans);
	free(to_send);

}

void throw_medication(avion_t aeroplane){
	char* to_send;
	int size = serialize_aeroplane(&to_send, aeroplane);
	void* ans=NULL;
	make_query(THROW_MEDICATION, to_send, size, &ans);
	if(ans){
		char* ansp= ans;
		int i;
		for(i=0 ; i<aeroplane->drogasCant ; i++){
			aeroplane->drogas[i]->cant=*((int*)ansp);
			ansp+=sizeof(int);
		}
		free(ans);
	}
	free(to_send);
}

int get_city_id(char* nombre){
	void* ans=NULL;
	make_query(GET_CITY_ID, nombre, strlen(nombre)+1, &ans);
	int ret;
	if(ans){
		ret=*(int*)ans;
		free(ans);
	}
	return ret;
}

void ready_to_draw(){
	void* ans=NULL;
	make_query(READY_TO_DRAW, NULL, 0, &ans);
	if(ans)	
		free(ans);
}

int get_neighbours(int** neighbours,int cityid){
	return make_query(GET_NEIGHBOURS, &cityid, sizeof(int), (void**)neighbours)/sizeof(int);
}

int needs(int city_id, int drogasCant, droga_t * drogas){
	int size = sizeof(int)*2;

	int i;
	for(i=0 ; i<drogasCant ; i++){
		size += strlen(drogas[i]->nombre) + 1 + sizeof(int);
	}

	char* to_send= malloc( size );
	char* to_sendp=to_send;

	*(int*)to_sendp= city_id;
	to_sendp += sizeof(int);

	*((int*)to_sendp)=drogasCant;
	to_sendp += sizeof(int);

	for(i=0 ; i<drogasCant ; i++){
		strcpy(to_sendp , drogas[i]->nombre);
		to_sendp += strlen(drogas[i]->nombre) + 1;

		*((int*)to_sendp)=drogas[i]->cant;
		to_sendp += sizeof(int);
	}

	void* ans=NULL; 
	make_query(NEEDS, to_send, size, &ans);
	int ret;
	if(ans){
		 ret=*(int*)ans;
		free(ans);
	}
	free(to_send);
	return ret;
}

int null_drugs_demand(){
	void* ans=NULL;
	make_query(NULL_DRUGS_DEMAND, NULL, 0, &ans);
	int ret;
	if(ans){
		 ret=*(int*)ans;
		free(ans);
	}
	return ret;
}

void close_main(){
	message_t message;
	message=marshal_query(CLOSE_MAIN, NULL, 0);
	ipc_msg_send(SERVER, &message);
	free(message.text);
}

int get_city_cant(){
	void* ans=NULL;
	make_query(GET_CITY_CANT, NULL, 0, &ans);
	int ret;
	if(ans){
		 ret=*(int*)ans;
		free(ans);
	}
	return ret;
}
